package com.hykj.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hykj.dao.IndexPicDao;
import com.hykj.toolsbean.StringHandler;
import com.hykj.valuebean.IndexPic;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class IndexPicServlet
 */
public class IndexPicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexPicServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();
		if (servletPath.equals("/admin/insertIndexPic")) {
			try {
				insertIndexPic(request, response);
			} catch (SmartUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (servletPath.equals("/admin/index")) {
			listIndexPic(request, response);
		} else if (servletPath.equals("/admin/updateIndexPic")) {
			try {
				updateIndexPic(request, response);
			} catch (SmartUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (servletPath.equals("/admin/viewIndexPic")) {
			try {
				viewIndexPic(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (servletPath.equals("/admin/deleteIndexPic")) {
			deleteIndexPic(request, response);
		}
	}
	
	protected void deleteIndexPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String Id = request.getParameter("Id");
		IndexPicDao indexPicDaoIns = new IndexPicDao();
		indexPicDaoIns.deletePic(Id);
		indexPicDaoIns.closed();
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/index");
		dispatcher.forward(request, response);
		
	}
	
	protected void viewIndexPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		IndexPicDao indexPicDaoIns = new IndexPicDao();
		IndexPic indexPicIns = new IndexPic();
		indexPicIns = indexPicDaoIns.getIndexPic(StringHandler.strToint(request.getParameter("id")));
		indexPicDaoIns.closed();
		request.setAttribute("indexPicIns", indexPicIns);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/updateIndexPic.jsp");
		dispatcher.forward(request, response);
	}
	
	protected void updateIndexPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SmartUploadException {
		IndexPicDao indexPicDaoIns = new IndexPicDao();
		IndexPic indexPicIns = new IndexPic();
		SmartUpload su = new SmartUpload();
		su.initialize(this.getServletConfig(), request, response);
		su.setMaxFileSize(2 * 1024 * 1024);
		su.upload();
		Files files = su.getFiles();
		File file = files.getFile(0);
		String fileType = file.getFileExt();
		String[] type = { "JPG", "jpg", "gif", "bmp", "BMP" };
		int place = java.util.Arrays.binarySearch(type, fileType);
		if (place != -1) {
			if (!file.isMissing()) {
				String dir = "images/goods/"
					+ System.currentTimeMillis() + "."
					+ file.getFileExt(); 
				indexPicIns.setDescribes(su.getRequest().getParameter("describes"));
				indexPicIns.setPicDir(dir);
				indexPicIns.setPicHref(su.getRequest().getParameter("picHref"));
				indexPicIns.setCategory(su.getRequest().getParameter("category"));
				indexPicIns.setSequ(su.getRequest().getParameter("sequ"));
				indexPicIns.setId(StringHandler.strToint(su.getRequest().getParameter("id")));
				
				indexPicDaoIns.updatePic(indexPicIns);
				indexPicDaoIns.closed();
				file.saveAs(dir, File.SAVEAS_VIRTUAL);
			}
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/index");
		dispatcher.forward(request, response);
	}
	
	protected void insertIndexPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SmartUploadException {
		IndexPicDao indexPicDaoIns = new IndexPicDao();
		IndexPic indexPicIns = new IndexPic();
		SmartUpload su = new SmartUpload();
		su.initialize(this.getServletConfig(), request, response);
		su.setMaxFileSize(2 * 1024 * 1024);
		su.upload();
		Files files = su.getFiles();
		File file = files.getFile(0);
		String fileType = file.getFileExt();
		String[] type = { "JPG", "jpg", "gif", "bmp", "BMP" };
		int place = java.util.Arrays.binarySearch(type, fileType);
		if (place != -1) {
			if (!file.isMissing()) {
				String dir = "images/goods/"
					+ System.currentTimeMillis() + "."
					+ file.getFileExt(); 
				indexPicIns.setDescribes(su.getRequest().getParameter("describes"));
				indexPicIns.setPicDir(dir);
				indexPicIns.setPicHref(su.getRequest().getParameter("picHref"));
				indexPicIns.setCategory(su.getRequest().getParameter("category"));
				indexPicIns.setSequ(su.getRequest().getParameter("sequ"));
				indexPicDaoIns.insertPic(indexPicIns);
				indexPicDaoIns.closed();
				file.saveAs(dir, File.SAVEAS_VIRTUAL);
			}
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/index");
		dispatcher.forward(request, response);
	}
	
	protected  void listIndexPic(HttpServletRequest request, HttpServletResponse response) {
		IndexPicDao indexPicDaoIns = new IndexPicDao();
		List<IndexPic> indexPicList = null;
		try {
			indexPicList = indexPicDaoIns.getAllIndexPic();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("indexPicList", indexPicList);
		RequestDispatcher rd = request.getRequestDispatcher("/admin/index.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
